package my.edu.model.User.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import my.edu.model.User.entity.Permission;
import my.edu.model.User.entity.RolePermission;
import my.edu.model.User.entity.UserRole;
import my.edu.model.User.mapper.PermissionMapper;
import my.edu.model.User.mapper.RoleMapper;
import my.edu.model.User.mapper.RolePermissionMapper;
import my.edu.security.userEntity.UserDetail;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;


    @Override
    public List<String> getAllUrl() {
        return list().stream().map(Permission::getUrl).toList();
    }

    @Override
    public List<String> getUrlByUserId(Long userId) {
        MPJLambdaWrapper<Permission> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(Permission::getUrl)
                .leftJoin(RolePermission.class,"roleper",RolePermission::getPermissionId,Permission::getId )
                .in(RolePermission::getRoleId, UserRole.class, eq->eq
                        .setAlias("userrole")
                        .select(UserRole::getRoleId)
                        .eq(UserRole::getUserId, userId));
        List<Permission> permissions = permissionMapper.selectJoinList(Permission.class, wrapper);
        return permissions.stream().map(Permission::getUrl).toList();
    }

    @Override
    public List<Permission> getRolePermission(Long roleId) {
        MPJLambdaWrapper<Permission> wrapper=new MPJLambdaWrapper<>(Permission.class);
        List<Permission> list = wrapper.selectAll(Permission.class)
                .leftJoin(RolePermission.class, RolePermission::getPermissionId, Permission::getId)
                .eq(RolePermission::getRoleId, roleId)
                .list();
        return list;
    }

    @Override
    public List<String> getAllName() {
        return list().stream().map(Permission::getName).distinct().toList();
    }

    @Override
    public List<String> getNameByUserId(Long userId) {
        MPJLambdaWrapper<Permission> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(Permission::getName)
                .leftJoin(RolePermission.class,"roleper",RolePermission::getPermissionId,Permission::getId )
                .in(RolePermission::getRoleId, UserRole.class, eq->eq
                        .setAlias("userrole")
                        .select(UserRole::getRoleId)
                        .eq(UserRole::getUserId, userId));

        List<Permission> permissions = permissionMapper.selectJoinList(Permission.class, wrapper);

        return permissions.stream().map(Permission::getName).distinct().toList();

    }

    @Override
    public void getMunu() {


    }

    @Override
    public List<Permission> getByUserId(Long userId) {
        MPJLambdaWrapper<Permission> wrapper = new MPJLambdaWrapper<>(Permission.class);
        wrapper.selectAll()
                .leftJoin(RolePermission.class,"roleper",RolePermission::getPermissionId,Permission::getId )
                .in(RolePermission::getRoleId, UserRole.class, eq->eq
                        .setAlias("userrole")
                        .select(UserRole::getRoleId)
                        .eq(UserRole::getUserId, userId));

        List<Permission> permissions = permissionMapper.selectJoinList(Permission.class, wrapper);
        return permissions;
    }
}
